﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HPortal.Lib;
using System.Data;
using System.Data.SqlClient;
using System.Web.Security;

namespace HPortal.Backend
{
    public partial class PanelList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindcboChannel();
                BindPanelList();
                BindcboControl();
                BindcboCell();
            }
            string panelID = txtPanelID.Text;
            if (panelID != "")
            {
                cmdSave.Enabled = true;
                cmdDelete.Enabled = true;
            }
            else
            {
                cmdSave.Enabled = false;
                cmdDelete.Enabled = false;
            }

        }
        private DataTable getChannel()
        {
            string where = string.Empty;
            if (this.Context.User.Identity.IsAuthenticated)
            {
                string[] arrRoleName = Roles.GetRolesForUser(this.Context.User.Identity.Name);
                foreach (string roleName in arrRoleName)
                {
                    where += DatabaseHelper.EscapeQuoteUnicode(roleName) + ",";
                }

            }
            where = where.Remove(where.Length - 1);
            where = "WHERE ChannelID IN (SELECT DISTINCT ChannelID FROM [ChannelRole] WHERE ([Role] IN (" + where + ") AND Administrated=1))";
            string query = "SELECT * FROM [Channel] " + where + " ORDER BY [ParentID],[Priority],[ChannelID]";
            return DatabaseHelper.Select(query);
        }
        private string getChannelName(int channelID)
        {
            int parentID;
            string name = DatabaseHelper.SelectScalarStringFromID("Name", "Channel", channelID); ;
            parentID = DatabaseHelper.SelectScalarIntFromID("ParentID", "Channel", channelID);
            while (parentID > 0)
            { 

                name ="__" + name;
                channelID = parentID;
                parentID = DatabaseHelper.SelectScalarIntFromID("ParentID", "Channel", channelID);
            }
            return name;
        }
        protected void BindcboChannel()
        {
            DataTable channel = getChannel();
            for (int i = 0; i < channel.Rows.Count; i++)
                channel.Rows[i]["Name"] = getChannelName(int.Parse(channel.Rows[i]["ChannelID"].ToString()));
            this.cboChannelList.DataSource = channel;
            this.cboChannelList.DataValueField = "ChannelID";
            this.cboChannelList.DataTextField = "Name";
            this.cboChannelList.DataBind();
        }
        protected void BindPanelList()
        {
            grvPanelList.DataSource = DatabaseHelper.Select("exec getPanelList " + cboChannelList.SelectedValue);
            grvPanelList.DataBind();
        }
        protected void BindcboControl()
        {
            this.cboControl.DataSource = DatabaseHelper.Select("SELECT ControlID,Name FROM Control");
            this.cboControl.DataValueField = "ControlID";
            this.cboControl.DataTextField = "Name";
            this.cboControl.DataBind();
            this.cboControl.SelectedIndex = 0;
        }
        protected void BindcboCell()
        {
            this.cboCell.DataSource = DatabaseHelper.Select("SELECT CellID,Description FROM Cell");
            this.cboCell.DataValueField = "CellID";
            this.cboCell.DataTextField = "Description";
            this.cboCell.DataBind();
            this.cboCell.SelectedIndex = 0;
        }
        protected void cboChannelList_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindPanelList();
        }

        protected void cmdSave_Click(object sender, EventArgs e)
        {
            DatabaseHelper.Update(new string[] { "N@Header", "@ControlID", "@CellID", "@Priority", "@DisplayContainer", "@DisplayInAllChannel", "N@Field1", "N@Field2" },
                new string[] { txtHeader.Text, cboControl.SelectedValue, cboCell.SelectedValue, txtPriority.Text, (chkDisplayContainer.Checked ? "1" : "0"), (chkDisplayInAllChannel.Checked ? "1" : "0"), (txtField1.Text.Equals("") ? "5" : txtField1.Text), (txtField2.Text.Equals("") ? "5" : txtField2.Text) },
                    "Panel", "[PanelID]=" + txtPanelID.Text);
            BindPanelList();
        }

        protected void cmdNew_Click(object sender, EventArgs e)
        {
            DatabaseHelper.Insert(new string[] { "N@Header", "@ControlID", "@ChannelID", "@CellID", "@Priority", "@DisplayContainer", "@DisplayInAllChannel", "N@Field1", "N@Field2" },
            new string[] { txtHeader.Text, cboControl.SelectedValue,cboChannelList.SelectedValue, cboCell.SelectedValue, txtPriority.Text, (chkDisplayContainer.Checked ? "1" : "0"), (chkDisplayInAllChannel.Checked ? "1" : "0"), (txtField1.Text.Equals("") ? "5" : txtField1.Text), (txtField2.Text.Equals("") ? "5" : txtField2.Text) },"Panel");
            BindPanelList();
        }

        protected void cmdDelete_Click(object sender, EventArgs e)
        {
            DatabaseHelper.Delete("Panel", "PanelID=" + txtPanelID.Text);
            BindPanelList();
        }

        protected void lnkChoose_Click(object sender, EventArgs e)
        {
            string panelID = ((LinkButton)sender).CommandArgument;
            if (panelID != "")
            {
                DataTable p = DatabaseHelper.Select("SELECT * FROM Panel WHERE PanelID=" + panelID);
                if (p.Rows.Count > 0)
                {
                    txtHeader.Text = p.Rows[0]["Header"].ToString();
                    txtPanelID.Text = p.Rows[0]["PanelID"].ToString();
                    txtPriority.Text = p.Rows[0]["Priority"].ToString();
                    txtField1.Text = p.Rows[0]["Field1"].ToString();
                    txtField2.Text = p.Rows[0]["Field2"].ToString();
                    cboCell.SelectedValue = p.Rows[0]["CellID"].ToString();
                    cboControl.SelectedValue = p.Rows[0]["ControlID"].ToString();
                }
                cmdSave.Enabled = true;
                cmdDelete.Enabled = true;
            }
            else
            {
                cmdSave.Enabled = false;
                cmdDelete.Enabled = false;
            }
        }

    }
}